package com.homegoods.login.ui.fragment;

import android.Manifest;
import android.annotation.SuppressLint;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.ArrayMap;
import android.widget.ArrayAdapter;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.login.R;
import com.homegoods.login.databinding.LoginPhoneFragmentBindingImpl;
import com.homegoods.login.evnet.LoginSucEvent;
import com.homegoods.login.model.UserProxy;
import com.homegoods.login.presenter.LoginPhonePresenter;
import com.homegoods.login.presenter.LoginPresenter;
import com.homegoods.login.view.LoginView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.kedll.library.fragment.KBaseFragment;
import com.kedll.library.util.KeyBoardUtils;
import com.kedll.library.util.MyLog;
import com.kedll.library.util.RxBus;
import com.kedll.library.util.StringUtil;
import com.kedll.library.util.ToastUtil;
import com.kedll.library.widget.AIAutoCompleteTextView;
import com.share.sharelibrary.ThirdLoginView;
import com.share.sharelibrary.util.ShareSDKHelp;
import com.trello.rxlifecycle2.android.FragmentEvent;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;
import java.util.Objects;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

/**
 * 账号密码登录
 */
@RuntimePermissions
@Route(path = ARouterPath.PATH_PHONE)
public class LoginPhoneFragment extends KBaseFragment<LoginPhoneFragmentBindingImpl, LoginPhonePresenter> implements LoginView {
//    private ThirdLoginView thirdLoginView;
    private AIAutoCompleteTextView mPhone;

    @SuppressLint("CheckResult")
    @Override
    protected void initEvent(Bundle savedInstanceState) {
        initData();

//        LoginActivityPermissionsDispatcher.requestUMWithPermissionCheck(this);
        RxBus.getDefault().toFlowable(LoginSucEvent.class)
                .compose(this.bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(loginSucEvent -> {
                    ToastUtil.showToast(R.string.login_suc);
                    ARouter.getInstance().build(ARouterPath.PATH_MAIN_HOME).navigation();
                });
        RxTextView.afterTextChangeEvents(mBinding.etPwd)
                .compose(bindToLifecycle())
                .subscribe(textChangeEvent -> mBinding.btnLogin.setEnabled(
                        Objects.requireNonNull(textChangeEvent.editable()).length() > 0
                                && StringUtil.isNotEmpty(mPhone.getText())));

        RxTextView.afterTextChangeEvents(mBinding.phone)
                .compose(bindToLifecycle())
                .subscribe(textChangeEvent -> mBinding.btnLogin.setEnabled(
                        Objects.requireNonNull(textChangeEvent.editable()).length() > 0
                                && StringUtil.isNotEmpty(mBinding.etPwd.getText())));
    }

    private void initData() {
        UserProxy.getInstance().init(getActivity());
        mBinding.setLoginPhone(mPresenter);
//        thirdLoginView = mBinding.thirdLogin;
        mPhone = mBinding.phone;

        //文字下划线
        mBinding.userAgreement.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        mBinding.userAgreement.getPaint().setAntiAlias(true);

//        thirdLoginView.setColumnNum(2);
//        thirdLoginView.hideHeadView();
//        Map<SHARE_MEDIA, Integer> map = new ArrayMap<>();
//        map.put(SHARE_MEDIA.WEIXIN, R.mipmap.wechat_login);
//        map.put(SHARE_MEDIA.QQ, R.mipmap.qq_login);

//        thirdLoginView.setPlatForm(map, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QQ);
        //QQ登录
//        thirdLoginView.setLogin(access_token -> mPresenter.qqLogin(access_token));
    }

    @Override
    protected LoginPhonePresenter getPresenter() {
        return new LoginPhonePresenter(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.login_phone_fragment;
    }

    @Override
    public void hideKeyBoard() {
        KeyBoardUtils.hideKeyBoard(getActivity());
    }

    @Override
    public void showRegisterView() {
        ARouter.getInstance()
                .build(ARouterPath.PATH_REGISTER)
                .navigation(getActivity(), 100);
    }

    @Override
    public void showRestPwdView() {
        ARouter.getInstance()
                .build(ARouterPath.PATH_REST_PWD)
                .navigation();
    }

    @Override
    public void setAutoTextViewAdapter(ArrayAdapter adapter) {

    }

    @Override
    public void showLoad(String msg) {
        MyLog.getInstance().showProgressDialog(getActivity(), msg);
    }

    @Override
    public void hideLoad() {
        MyLog.getInstance().dismissDialog();
    }

    @NeedsPermission({Manifest.permission.READ_CALENDAR, Manifest.permission.GET_ACCOUNTS, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void requestUM() {
    }
}
